package com.dubbo.server.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.dubbo.server.pojo.TimeLine;
import com.tanhua.dubbo.server.pojo.Users;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;

@Service
@Slf4j
public class TimeLineService1 {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Async
    public CompletableFuture<String> saveTimeLine(Long userid, ObjectId publishId){
        try {
            Query query =new Query(Criteria.where("userId").is(userid));
            List<Users> users = mongoTemplate.find(query, Users.class);
            if(CollUtil.isEmpty(users))
                return  CompletableFuture.completedFuture("ok");
            System.out.println(users);
            for (Users user :
                    users) {
                TimeLine timeLine = new TimeLine();
                timeLine.setId(ObjectId.get());
                timeLine.setDate(System.currentTimeMillis());
                timeLine.setPublishId(publishId);
                timeLine.setUserId(userid);



                mongoTemplate.save(timeLine,"quanzi_time_line_"+user.getFriendId());

            }

        } catch (Exception e) {
            log.error("保存用户时间线表错误userId = " + userid + ", publishId = " + publishId, e);
            return CompletableFuture.completedFuture("error");
        }
        return CompletableFuture.completedFuture("ok");
    }
}
